/**
 * com.flute.haflute.web是为web管理提供信息服务的后台工具包
 */
package com.flute.haflute.web;

import java.util.ArrayList;
import java.util.Map;
import java.util.Map.Entry;

import com.flute.haflute.agent.AgentContext;
import com.flute.haflute.agent.job.JobContext;
import com.flute.haflute.agent.job.JobResourceManager;
import com.flute.haflute.agent.topo.ClusterTopoDBService;
import com.flute.haflute.agent.topo.Node;
import com.flute.haflute.common.ClusterConstants.ROLE;
import com.flute.haflute.common.ClusterContext;

/**
 * 为Web管理服务提供topo查询的接口类
 */
public class TopoQuery {

	/**
	 * 获取DM以及状态
	 */
	public static NodeEntity getDM() {
		String dM = ClusterContext.getDM();
		NodeEntity ne = new NodeEntity();
		ne.setIp(dM);
		ne.setRole("指定主节点");

		Node node = new Node(dM);
		if (JobResourceManager.getInstance().getAllFreeNodes().contains(node)) {
			ne.setNodeIdleState("<font color='green'>空闲</font>");
		} else {
			JobContext ctx = JobResourceManager.getInstance()
					.getBusyJobContext4Node(node);
			if (ctx != null)
				ne.setNodeIdleState("<font color='blue'>" + ctx.getNumOfJob()
						+ "个作业</font>");
			else
				ne.setNodeIdleState("<font color='blue'>忙碌</font>");
		}

		if (AgentContext.getNodeRole() == ROLE.DM) {
			ne.setStatus("<font color='green'>正常</font>");
		} else {
			ROLE role = ClusterTopoDBService.getInstance().getNodeRoleDB()
					.get(node);
			if (role == null) {
				ne.setStatus("<font color='red'>故障</font>");
				ne.setNodeIdleState("<font color='blue'>N/A</font>");
			} else {
				ne.setStatus("<font color='green'>正常</font>");
			}
		}
		return ne;
	}

	/**
	 * 获取BDM以及状态
	 */
	public static NodeEntity getBDM() {
		
		String bDM = ClusterContext.getBDM();
		
		NodeEntity ne = new NodeEntity();
		
		ne.setIp(bDM);
		ne.setRole("指定备份节点");

		Node node = new Node(bDM);
		if (JobResourceManager.getInstance().getAllFreeNodes().contains(node)) {
			ne.setNodeIdleState("<font color='green'>空闲</font>");
		} else {
			JobContext ctx = JobResourceManager.getInstance()
					.getBusyJobContext4Node(node);
			if (ctx != null)
				ne.setNodeIdleState("<font color='blue'>" + ctx.getNumOfJob()
						+ "个作业</font>");
			else
				ne.setNodeIdleState("<font color='blue'>忙碌</font>");
		}
		if (AgentContext.getNodeRole() == ROLE.BDM) {
			ne.setStatus("<font color='green'>正常</font>");
		} else {
			ROLE role = ClusterTopoDBService.getInstance().getNodeRoleDB()
					.get(node);
			if (role == null) {
				ne.setStatus("<font color='red'>故障</font>");
				ne.setNodeIdleState("<font color='blue'>N/A</font>");
			} else {
				ne.setStatus("<font color='green'>正常</font>");
			}
		}

		return ne;
	}

	/**
	 * 获取所有非DM/BDM节点的角色以及状态
	 */
	public static NodeEntity[] getOrdinaryNodes() {
		
		ArrayList<NodeEntity> nodeSet = new ArrayList<NodeEntity>();
		
		Map<Node, ROLE> db = ClusterTopoDBService.getInstance().getNodeRoleDB();
		
		for (Entry<Node, ROLE> entry : db.entrySet()) {
			
			ROLE role = entry.getValue();
			if (role == ROLE.SM || role == ROLE.SLAVE) {
				NodeEntity node = new NodeEntity();
				node.setRole(role == ROLE.SM ? "选择主节点" : "从节点");
				node.setIp(entry.getKey().getIp());
				node.setStatus("<font color='green'>正常</font>");
				if (JobResourceManager.getInstance().getAllFreeNodes()
						.contains(entry.getKey())) {
					node.setNodeIdleState("<font color='green'>空闲</font>");
				} else {
					JobContext ctx = JobResourceManager.getInstance()
							.getBusyJobContext4Node(entry.getKey());
					if (ctx != null)
						node.setNodeIdleState("<font color='blue'>"
								+ ctx.getNumOfJob() + "个作业</font>");
					else
						node.setNodeIdleState("<font color='blue'>忙碌</font>");
				}
				nodeSet.add(node);
			}
		}

		return nodeSet.toArray(new NodeEntity[nodeSet.size()]);
	}

}
